package es.xpt.ysf.commons.location.view;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIData;
import javax.faces.event.ValueChangeEvent;

import es.xpt.ysf.YSFException;
import es.xpt.ysf.commons.location.model.Address;
import es.xpt.ysf.commons.location.model.City;
import es.xpt.ysf.commons.location.model.Country;
import es.xpt.ysf.commons.location.model.Province;
import es.xpt.ysf.commons.location.view.LocationDelegate;
import es.xpt.ysf.logging.LogManager;

@SuppressWarnings("serial")
public class AddressController extends AddressList { // NO_UCD
	
	private String id;
	
	private Address searchAddress;
	
	private ArrayList<String[]> entities;
//	private UIData entityInfo;
	
	private String dependenceId;
	private List<String> dependences;
	private UIData dependenceInfo;
	private String entityUrl;
	
	public AddressController() {
		super();
		searchAddress = new Address();
		Country c = new Country();
		c.setCode("");
		searchAddress.setCountry(c);
		Province p = new Province();
		p.setCode("");
		searchAddress.setProvince(p);
		searchAddress.setCity(new City());
	}
	
	@Override public void updateModel (ValueChangeEvent e) {
		if ( getFacesContext().getViewRoot().getViewId().equals("/addressSearch.jspx") )
			super.updateModel(e, searchAddress);
		else super.updateModel(e, address);
	}
	
	@Override
	public Address getAddress() {
		if ( ( address == null || address.getId()==null) && id!=null && id.trim().length()>0) {
			try {
				address = new LocationDelegate(getUserBean()).getAddress(id);
				helper.setEditMode(true);
			} catch (YSFException e) {
				LogManager.getLog(getClass()).error(e.getMessage(), e);
			}
		}
		return address;
	}
	
	public List<Address> searchAddress(){
		addresses = null;
		if ( searchAddress!=null) {
			try {
				if (searchAddress.getCity().getCode() == null) searchAddress.getCity().setCode("");
				if (searchAddress.getCountry().getCode() == null) searchAddress.getCountry().setCode("");
				if (searchAddress.getProvince().getCode() == null) searchAddress.getProvince().setCode("");
				addresses = new LocationDelegate(getUserBean()).searchAddress(searchAddress);
			} catch (YSFException e) {
				LogManager.getLog(getClass()).error(e.getMessage(), e);
			}
		}
		return addresses;
	}

	public String updateAddress() {
		try {
			  new LocationDelegate(getUserBean()).updateAddress(address);
			} catch (YSFException e) {
				super.setErrorMessage(e);
				LogManager.getLog(getClass()).error(e.getMessage(), e);
				return "failure";
			}
			super.setInfoMessage("commons.address.update");
			addresses = null;
			return "success";
	}
	
	public String cancelEdit() {
		helper.setEditMode(false);
		addresses = searchAddress();
		return "success";
	}
	
	public String deleteAddress() {
		address = (Address)helper.getHtmlDataTable().getRowData();
			try {
			  new LocationDelegate(getUserBean()).deleteAddress(address.getId());
			} catch (YSFException e) {
				super.setErrorMessage(e);
				LogManager.getLog(getClass()).error(e.getMessage(), e);
				return "failure";
			}
		super.setInfoMessage("commons.address.remove");
		addresses = searchAddress();
		return "success";
	}	

	public String deleteAddressList() {
		LocationDelegate l1 = new LocationDelegate(getUserBean());
		 if (!helper.getSelectedAddressList().isEmpty()){
			 List<String> listMessages;
			 try {
				 listMessages = l1.deleteBatchAddress(helper.getSelectedAddressList()); 
			 	 if (listMessages==null || listMessages.isEmpty()){
			 		super.setInfoMessage("commons.address.remove");
			 	 }else if( listMessages.size()>0) {
			 		for(int i=0;listMessages.size()>=(i+1);i=i+2){
			 			Address addressTemp = new Address();
			 			addressTemp.setId(listMessages.get(i+1));
			 			int j=addresses.indexOf(addressTemp);
			 			super.setErrorMessage(listMessages.get(i),addresses.get(j).getAddress());
			 		}
			 	 }
			 } catch (YSFException e) {
					super.setErrorMessage(e);							
					LogManager.getLog(getClass()).error(e.getMessage(), e);
			}
		 }
		 addresses = searchAddress();		

		return "success";
	}

	public List<String> showDependences() {
		if (address != null){
			entity = null;
			entity = ((String[]) helper.getData().getRowData())[1];
			if (entity != null){
				try {
				  dependences = new LocationDelegate(getUserBean()).searchEntityDependences(address.getId(),entity);
				} catch (YSFException e) {
					super.setErrorMessage(e);
					LogManager.getLog(getClass()).error(e.getMessage(), e);
				}
			}
		}
		return dependences;
	}
	

	public ArrayList<String[]> getEntities() {
		if (entities == null && address!=null && address.getId()!=null){
		try {
			  entities = new LocationDelegate(getUserBean()).searchDependentEntities(address.getId());
			} catch (YSFException e) {
				super.setErrorMessage(e);
				LogManager.getLog(getClass()).error(e.getMessage(), e);
			}
		}
		return entities;
	}
	
	public String getEntityUrl() {
		return entityUrl;
	}
	
// GETTERS and SETTERS
	public String getDependenceId() {
		return dependenceId;
	}

	public void setDependenceId(String dependenceId) {
		this.dependenceId = dependenceId;
	}

	public Address getSearchAddress() {
		return searchAddress;
	}

	public void setSearchAddress(Address searchAddress) {
		this.searchAddress = searchAddress;
	}

	public void setEntities(ArrayList<String[]> entities) {
		this.entities = entities;
	}

	public List<String> getDependences() {
		return dependences;
	}


	public void setDependences(List<String> dependences) {
		this.dependences = dependences;
	}


	public UIData getDependenceInfo() {
		return dependenceInfo;
	}


	public void setDependenceInfo(UIData dependenceInfo) {
		this.dependenceInfo = dependenceInfo;
	}


	public void setEntityUrl(String entityUrl) {
		this.entityUrl = entityUrl;
	}


	public String getId() {
		return id;
	}


	public void setId(String id) {
		this.id = id;
	}
}



